<%@include file="/WEB-INF/views/includes/taglibs.jsp"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@include file="/WEB-INF/views/includes/adminHeader.jsp"%>
<script type="text/javascript">
function addParameter() {
	var currentConditionIndex = $('#parameterCount').val();
	var nextIndex = parseInt(currentConditionIndex) + 1;
	var staticHtml = '<div id="parameterblock' + currentConditionIndex + '" class="row ';
	if (nextIndex%2 == 0) {
		staticHtml = staticHtml + 'l2';
	} else {
		staticHtml = staticHtml + 'l1';
	}
	staticHtml = staticHtml + '">';
	
	staticHtml = staticHtml
	+ '<input type="hidden" name="parameters[' + currentConditionIndex + '].id" value="0" id="parameters[' + currentConditionIndex + '].id"/>'
	+ '<input type="hidden" value="' + currentConditionIndex + '" id="tempid' + currentConditionIndex + '"/>'
	+ '<input type="hidden" name="parameters[' + currentConditionIndex + '].functionDBID" value="${command.functionDBID}" id="parameters[' + currentConditionIndex + '}].functionDBID"/>'
	+ '<input type="hidden" name="parameters[' + currentConditionIndex + '].deleted" value="0" id="parameters[' + currentConditionIndex + '].deleted"/>'
	<c:forEach items="${command.parameterDefine.parameterElements}" var="paramaterDefineElement" varStatus="currentStep">
	+ '<b>${paramaterDefineElement.label}</b>&nbsp;'
	+ '<input style="width:120px" type="text" value="" '
	+ 'name="parameters[' + currentConditionIndex + '].parameterName" '
	+ 'id="parameters[' + currentConditionIndex + '].parameterName"/>&nbsp;'
	</c:forEach>
	+ '<a href="#" onclick="deleteParameter(' + currentConditionIndex + ')">Delete</a>';

	$('#parameterCount').val(nextIndex);
	$('#parametersTable').append(staticHtml);
}

function deleteParameter(tempid) {
	$('#parameterblock' + tempid).css("display","none");
	$('#parameters\\[' + tempid + '\\]\\.deleted').val('1');
}

function saveChanges() {
	$('#functionParameterForm').submit();
}
</script>
<div class="homePage ui-tabs-panel ui-widget-content ui-corner-bottom">
	<div id="leftMenu">
		<%@include file="/WEB-INF/views/includes/functionAdminLeftNav.jsp"%>
	</div>
	<form:form method="post" action="saveFunctionParameters" id="functionParameterForm">
	<div id="rightContent">
		<%@include file="/WEB-INF/views/includes/messages.jsp"%>
		<h2>Parameters of Function ${command.functionName}</h2>
		<form:hidden path="functionId"/>
		<form:hidden path="functionDBID"/>
		<div class="searchBlock">
				<button type="button"
						class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
						onclick="saveChanges()" style="margin-top:5px">
						<span class="ui-button-text">Save changes</span>
				</button>
				<button type="button"
						class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
						onclick="addParameter()" style="margin-top:5px">
						<span class="ui-button-text">Add new parameter</span>
				</button>
		</div>
		<div>
			
		</div>
		<div class="newtable" id="parametersTable">
			<input type="hidden" name="parameterCount" value="${fn:length(command.parameters)}" id="parameterCount"/>
			<c:forEach items="${command.parameters}" var="oneParamater" varStatus="currentStep">
			<div id="parameterblock${currentStep.count - 1}"
			<c:if test="${currentStep.count%2==0}">
				class="row l2"
			</c:if>
			<c:if test="${currentStep.count%2!=0}">
			    class="row l1"
			</c:if>
			>
				<input type="hidden" name="parameters[${currentStep.count - 1}].id" value="${oneParamater.id}" id="parameters[${currentStep.count - 1}].id"/>
				<input type="hidden" value="${currentStep.count - 1}" id="tempid${currentStep.count - 1}"/>
				<input type="hidden" name="parameters[${currentStep.count - 1}].functionDBID" value="${oneParamater.functionDBID}" id="parameters[${currentStep.count - 1}].functionDBID"/>
				<input type="hidden" name="parameters[${currentStep.count - 1}].deleted" value="0" id="parameters[${currentStep.count - 1}].deleted"/>
				<b>Name:</b>&nbsp;<input style="width:120px" type="text" value="${oneParamater.parameterName}" 
				name="parameters[${currentStep.count - 1}].parameterName" 
				id="parameters[${currentStep.count - 1}].parameterName"/>&nbsp;<b>Type:</b>&nbsp;<input style="width:120px" type="text" value="${oneParamater.parameterType}" 
				name="parameters[${currentStep.count - 1}].parameterType" 
				id="parameters[${currentStep.count - 1}].parameterType"/>&nbsp;<b>Value:</b>&nbsp;<input style="width:220px" type="text" value="${oneParamater.parameterValue}" 
				name="parameters[${currentStep.count - 1}].parameterValue" 
				id="parameters[${currentStep.count - 1}].parameterValue"/>&nbsp;&nbsp;<a href="#" onclick="deleteParameter(${currentStep.count - 1})">Delete</a>
			</div>
			</c:forEach>
		</div>
	</div>
	</form:form>
</div>

<%@include file="/WEB-INF/views/includes/adminfooter.jsp"%>